package com.qq.BFMRSE.frame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.qq.BFMRSE.encrypt.encryotHelper;
public class keyGenFrame extends JFrame implements ActionListener{
	private JLabel inputLabel;
	private JLabel keyPathLabel;
	private static JTextField keyGenInputTF;
	private static JTextField keyPathTF;
	private JButton keyGenBtn ;
	private JButton choseBtn;
	private JFileChooser jfc;
	/**
	 * Create the frame.
	 */
	public keyGenFrame() {
		init();
		this.setTitle("DES秘钥生成界面");
		getContentPane().setLayout(null);
		jfc.setCurrentDirectory(new File("d://"));// 文件选择器的初始目录定为d盘
		choseBtn.addActionListener(this);
		keyGenBtn.addActionListener(this);
		setSize(500, 300);
		setVisible(true);
		setResizable(true);// 禁止拉边框拉长拉断
		setLocationRelativeTo(null);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
        this.setIconImage(imgBF.getImage()); 
	}
	public void init(){
		inputLabel = new JLabel("秘钥生成参数：");
		keyPathLabel= new JLabel("秘钥存储目录：");
		keyGenInputTF = new JTextField(10);
		keyPathTF= new JTextField(10);
		keyGenBtn = new JButton("开始生成秘钥并保存");
		inputLabel.setBounds(79, 72, 114, 30);
		getContentPane().add(inputLabel);
		keyPathLabel.setBounds(79, 122, 114, 30);
		getContentPane().add(keyPathLabel);
		keyGenInputTF.setBounds(189, 75, 100, 25);
		getContentPane().add(keyGenInputTF);
		keyPathTF.setBounds(189, 125, 100, 25);
		getContentPane().add(keyPathTF);
		keyGenBtn.setBounds(79, 172, 299, 30);
		getContentPane().add(keyGenBtn);
		jfc = new JFileChooser();// 文件选择器
	    choseBtn = new JButton("选择");
		choseBtn.setBounds(314, 126, 64, 25);
		getContentPane().add(choseBtn);
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					keyGenFrame frame = new keyGenFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==choseBtn){
			jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File skFile=jfc.getSelectedFile();
				keyPathTF.setText(skFile.getAbsolutePath());
			}
		}else if(e.getSource()==keyGenBtn){
			//进行秘钥生成并保存到文件中
			String keyGenInput=keyGenInputTF.getText();
			String keyPath=keyPathTF.getText();
			if("".equals(keyGenInput)||"".equals(keyPath)){
				JOptionPane.showMessageDialog(this, "请输入参数或选择秘钥存放目录！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}else{
				encryotHelper.keyGenAndSava(keyGenInput, keyPath);
				JOptionPane.showMessageDialog(this, "秘钥生成结束，并成功保存！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}
